﻿Imports System.Data.Linq
Imports System.Linq

Public Class MCurrencies
    Private db As New TanNamChinhDataContext
    Public Function getCurrenciesList1() As Table(Of tblCurrency)
        Try
            Return db.tblCurrencies
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function getCurrenciesList2() As IQueryable
        Try
            Dim query = From b In db.tblCurrencies
                        Where b.IsActive = True
                        Order By b.ID_Currency Descending
                        Select b.ID_Currency, b.Code, b.Name
            'Return (From r In db.tblCurrencies
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getCurrenciesList2(ByVal CurrCode As String) As IQueryable
        Try
            Dim query = From b In db.tblCurrencies
                        Where b.IsActive = True And b.Code.Contains(CurrCode)
                        Order By b.ID_Currency Descending
                        Select b.ID_Currency, b.Code, b.Name
            'Return (From r In db.tblCurrencies
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#Region "Thanh - update 24/11/2013"
    Public Function GetCurrencyByCode(ByVal sCode As String) As tblCurrency
        Try
            Return (From c In db.tblCurrencies
                    Where c.Code = sCode
                    Select c).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function GetCurrencyByID(ByVal id As Integer) As tblCurrency
        Try
            Return (From c In db.tblCurrencies
                    Where c.ID_Currency = id
                    Select c).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
#End Region

#Region "Tuan add 23/12/2013"
    ''' <summary>
    ''' Lấy danh sách Currency trả về object kiểu tblCurrency
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectAllRecord() As IQueryable(Of tblCurrency)
        Dim query = From p In db.tblCurrencies
                     Where p.IsActive = True
                     Select p
        Return query
    End Function
#End Region

    'Public Function getCurrencyExchangeList() As IQueryable
    '    Try
    '        Dim query = From b In db.tblCurrencyExchanges
    '                    Order By b.ExchangeDate Descending
    '                    Select b.ID_Currency, b.Ratio, b.ExchangeDate, b.ID_CurrencyExchange
    '        'Return (From r In db.tblCurrencies
    '            '           Select r).Single
    '        Return query
    '    Catch ex As Exception
    '        Throw ex
    '    End Try
    'End Function
    Public Sub InsertNewRecord(ByVal obj As tblCurrency)
        db.tblCurrencies.InsertOnSubmit(obj)
        'db.GetTable(Of tblCurrency)().InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub
    Public Sub UpdateRecord(ByVal obj As tblCurrency)
        Dim getData = (From b In db.tblCurrencies
                                Where b.ID_Currency = obj.ID_Currency
                                Select b).Single()
        getData.Code = obj.Code
        getData.Name = obj.Name
        'getData.Name2 = obj.Name2
        'getData.Ratio = obj.Ratio
        'getData.Description = obj.Description
        db.SubmitChanges()

    End Sub
    Public Sub DeleteUpdateRecord(ByVal id As Integer)
        Dim getData = (From b In db.tblCurrencies
                                Where b.ID_Currency = id
                                Select b).Single()

        getData.IsActive = False
        db.SubmitChanges()

    End Sub
    Public Sub DeleteRecord(ByVal id As Integer)
        Dim getData = (From b In db.tblCurrencies
                                Where b.ID_Currency = id
                                Select b).Single()
        db.tblCurrencies.DeleteOnSubmit(getData)
        db.SubmitChanges()
    End Sub
    'Public Function GetNewestPrice(ByVal idCurrency As Integer) As Double
    '    Dim prices As Double
    '    Try
    '        prices = (From b In db.tblCurrencyExchanges
    '                 Where b.ID_Currency = idCurrency
    '                 Order By b.ExchangeDate Descending
    '                 Select b.Ratio).First()
    '    Catch ex As Exception
    '        Return 0
    '    End Try
    '    Return prices
    'End Function
    'Public Function GetPrice(ByVal id_currencyE As Integer) As Double
    '    Dim prices As Double
    '    Try
    '        prices = (From b In db.tblCurrencyExchanges
    '                 Where b.ID_CurrencyExchange = id_currencyE
    '                 Order By b.ExchangeDate Descending
    '                 Select b.Ratio).First()
    '    Catch ex As Exception
    '        Return 0
    '    End Try
    '    Return prices
    'End Function
    'Public Function GetNewestIDCurrencyEx(ByVal idCurrency As Integer) As Double
    '    Dim prices As Double
    '    Try
    '        prices = (From b In db.tblCurrencyExchanges
    '                 Where b.ID_Currency = idCurrency
    '                 Order By b.ExchangeDate Descending
    '                 Select b.ID_CurrencyExchange).First()
    '    Catch ex As Exception
    '        Return 0
    '    End Try
    '    Return prices
    'End Function
End Class
